# encoding=utf-8
from django.shortcuts import render
import requests
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from .setting import *
from login.verify import *
# Create your views here.


def index(request):
    if request.method == "POST":
        info = dict(request.POST)
        print(info)
        user, pswd = info["user_name"][0], info["pass_word"][0]
        login_api = url_ip + 'login/api_login/'
        print(login_api)
        r = requests.post(login_api, data={"user": user, "pswd": pswd})
        print(r.text)
        if "登陆成功" == r.text:
            return HttpResponseRedirect("https://www.baidu.com/")
        else:
            context = {}
            return render(request, "apilogin/index.html", context)
    elif request.method == "GET":
        context = {}
        return render(request, "apilogin/index.html", context)


def register(request):

    """
    {'user_account': ['qwe'], 'user_name': ['qwesa'], 'di_code': ['adsad'], 'phone':
   ['asdsad'], 'api_pswd': ['adsa'], 'sex': ['男']}

   'user_code': info['user_code'][0],
   'user_name': info['user_name'][0],
   'user_id': info['user_id'][0],
   'phone': info['phone'][0],
   'api_pswd': info['api_pswd'][0],
   'sex': info['sex'][0]

    """
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        login_api = url_ip + 'login/api_register/'
        print(login_api)
        r = requests.post(login_api, data=info)
        if '注册成功' == r.text:
            return HttpResponseRedirect(reverse("apilogin:register"))
        else:
            return HttpResponseRedirect(reverse("apilogin:register"))
    elif request.method == 'GET':
        context = {}
        return render(request, 'apilogin/register.html', context)
    

    










    

    
